#!/usr/bin/php
<?php

/* Adding Object Oreinted functionality to the bot :o
   Functions FTW! */

function send($cmd ) 
{
	global $irc, $irc_var, $data, $channel;
	$cmd = str_replace(array("\r","\n"),'',$cmd); //Take out extraneous returns
	fwrite( $irc , $cmd . "\r\n" );
	echo "[" . date("G:i:s:") . "] >>> $cmd\r\n"; 
}

function hidden($cmd ) 
{
	global $irc, $irc_var, $data, $channel;
	fwrite( $irc, $cmd . "\r\n" );
}

function msg($msg ) 
{
	global $irc, $irc_var, $data, $channel;
	
	send("PRIVMSG $target :$msg");
}

function ctcp($ctcp )
{
	global $irc, $irc_var, $data, $channel;

	msg( chr(001) . $ctcp . ctcp(001) );
}

function respond($msg, $target = '')
{
	global $irc, $irc_var, $data, $channel, $d, $nick;
	if($target == '') {
		$target = ($channel == $botnick) ? $nick : $channel;
	}
	if(substr($target,0) == "#") {
		send("PRIVMSG $target :"."$nick".":".$msg);
	}
	else {
		send("PRIVMSG $target :$msg");
	}
}
function notice($msg, $target = '' )
{
	global $botnick, $d, $nick, $irc, $irc_var;
	if($target == ''){
		$target = ($d[2] == $botnick) ? $nick : $d[2];
	}
	send("NOTICE $target :$msg");
}
function ctcp_out($ctcp )
{
	global $irc, $irc_var, $data, $channel;
	
	notice( chr(001) . $ctcp . chr(001) );
}

function join_chan($channel, $key = '')
{
	if($key == ''){
		send("JOIN $channel");
	} else {
		send("JOIN $channel $key");
	}
	$irc_var["chans"] .= "," . $join; //Keep track of our channels
}

function part_chan($channel, $msg = '')
{
	global $irc_var;
	send("PART $channel :$msg\r\n");
	$i = 0; //Keep track of our channels
	$chansArr = explode(",",$irc_var["chans"]);
	foreach($chansArr as $key){
		if(stristr($channel,$chansArray)){
			unset($chansArray[$i]);
			$irc_var["chans"] = join(",",$chansArr);
		}
		$i++;
	}
}

function quit($msg = '', $net = '')
{	//$net is going to be used when the bot gains multi network abilities
	send("QUIT :$msg");
}

#TODO: Add a cmd function

?>
